package com.china08.yxyapi.api.manage.tec.teaching.mistakes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.mistakes.TeaMistakesQues;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.repository.es.ElasticYxyYxyQuesResourceRespository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesQuesRepository;
import com.china08.yxyapi.util.YxyResUtil;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 教师查看错题（多条件）
 * 
 * @author Administrator
 *
 */
@CrossOrigin
@RestController
public class TeaMistakesQuesPageController extends YxyApiBaseController {

    @Autowired
    private TeaMistakesQuesRepository teaMistakesQuesRepository;

    @Autowired
    private ElasticYxyYxyQuesResourceRespository elasticYxyYxyQuesResourceRespository;

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private YxyResUtil yxyResUtil;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/mistakes/mistakesquespage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) throws JsonParseException, JsonMappingException, IOException {
        String publishUserId = getProfile(request, response).getId();

        List<String> bookcontentList = yxyResUtil.findTocByTocParent(model.bookContentId);

        List<YxyResBookContentsKnoledges> knowIdLst = yxyResBookContentsKnoledgesRepository
                .findByBookContentIdIn(bookcontentList);
        List<Integer> know = new ArrayList<>();
        if (knowIdLst.size() > 0) {
            for (YxyResBookContentsKnoledges yrbcK : knowIdLst) {
                know.add(yrbcK.getKnowledgeId());
            }
        }
        // 题型选择
        List<String> questionType = new ArrayList<>();
        if (model.questionType == null) {
        } else if (model.questionType.equals("other")) {
            questionType.add("1");
            questionType.add("0");
            questionType.add("3");
            questionType.add("4");
        } else if (model.questionType.equals("fill")) {
            questionType.add("3");
            questionType.add("4");
        } else {
            questionType.add(model.questionType);
        }
        // 状态选择
        List<Integer> status = new ArrayList<>();
        if (model.status == null) {
            status.add(0);
            status.add(1);
        } else {
            status.add(model.status);
        }

        Pageable pageable = model.getPageable(Direction.DESC, "createdDate");
        Page<TeaMistakesQues> tmqPage;
        if (null == model.questionType || model.questionType.equals("other")) {
            tmqPage = teaMistakesQuesRepository
                    .findByClassIdAndPublishUserIdAndKnowIdLstInAndQuestionTypeNotInAndStatusIn(model.classId,
                            publishUserId, know, questionType, status, pageable);
        } else {
            tmqPage = teaMistakesQuesRepository.findByClassIdAndPublishUserIdAndKnowIdLstInAndQuestionTypeInAndStatusIn(
                    model.classId, publishUserId, know, questionType, status, pageable);
        }
        List<RespModel> list = new ArrayList<TeaMistakesQuesPageController.RespModel>();
        for (TeaMistakesQues tmq : tmqPage) {
            RespModel respModel = new RespModel();
            respModel.id = tmq.getId();
            ElasticYxyYxyQuesResource eyyqr = elasticYxyYxyQuesResourceRespository.findById(tmq.getQuesId());
            respModel.ques = eyyqr == null ? null : eyyqr.getQuesBody();
            respModel.sources = tmq.getSources();
            respModel.knowNameLst = tmq.getKnowNameLst();
            respModel.status = tmq.getStatus();
            respModel.mistakesNumber = tmq.getMistakesNumber();
            respModel.questionType = tmq.getQuestionType();
            list.add(respModel);
        }
        Page<RespModel> page = new PageImpl<TeaMistakesQuesPageController.RespModel>(list, pageable,
                tmqPage.getTotalElements());
        return page;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // 教材章节id
        public String bookContentId;

        // 班级id
        public String classId;

        // 试题类型，使用题库中的的类型 （0、单选择题，1、多选择题，2、填空, 3、解答）
        public String questionType;

        // 解决状态
        public Integer status;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {
        // id
        public String id;

        // 试题:对象
        public Object ques;

        // 题型类型 0、单选 3、4、填空 1、多选 剩余为、解答
        public String questionType;

        // 错题来源, 可能来源多个 00--作业系统 01--测评系统
        public List<String> sources;

        // 知识点
        public List<String> knowNameLst;

        // 0--未解决 1--已解决
        public Integer status;

        // 错误人数
        public Integer mistakesNumber;
    }
}
